package com.yaowk.service.mall.clothing.service.impl;

import com.yaowk.api.common.PageReq;
import com.yaowk.api.common.PageResp;
import com.yaowk.api.mall.clothing.model.Member;
import com.yaowk.api.mall.clothing.service.IMemberService;
import com.yaowk.api.mall.clothing.web.MemberModel;
import com.yaowk.service.common.BaseRepository;
import com.yaowk.service.common.BaseServiceImpl;
import com.yaowk.service.mall.clothing.dao.MemberDao;
import com.yaowk.service.mall.clothing.entity.CmMember;
import com.yaowk.service.mall.clothing.entity.CmMemberGroup;
import com.yaowk.service.mall.clothing.entity.CmMemberLevel;
import com.yaowk.service.mall.clothing.entity.CmSurveyor;
import com.yaowk.service.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MemberServiceImpl extends BaseServiceImpl<Member, CmMember, Integer> implements IMemberService {

    @Autowired
    private MemberDao dao;
    @Autowired
    private PageUtils pageUtils;

    @Override
    public PageResp<MemberModel> findAllOfWeb(String keyword, PageReq pageReq) {
        // 查询列表
        PageRequest pageRequest = pageUtils.pageReq2PageRequest(pageReq);
        CmMember condition = new CmMember();
        if (isNotEmpty(keyword)) {
            keyword = "%" + keyword + "%";
            condition.setCode(keyword);
            condition.setRealName(keyword);
        }
        Page<CmMember> page = dao.findAll(Example.of(condition), pageRequest);

        // 构造返回
        PageResp<MemberModel> pageResp = pageUtils.page2PageResp(page);

        List<MemberModel> memberModels = pageResp.getList();

        for (CmMember cmMember : page.getContent()) {
            MemberModel memberModel = new MemberModel();
            memberModel.setId(cmMember.getId());
            memberModel.setCode(cmMember.getCode());
            memberModel.setRealName(cmMember.getRealName());
            memberModel.setGroupName(cmMember.getMemberGroup().getName());
            memberModel.setLevelName(cmMember.getMemberLevel().getName());
            memberModel.setCreateTime(cmMember.getCreateTime());
            memberModel.setIntegrate(cmMember.getIntegrate());
            memberModel.setTurnoverOrder(cmMember.getTurnoverOrder());
            memberModel.setSurveyorName(cmMember.getSurveyor().getRealName());
            memberModels.add(memberModel);
        }

        return pageResp;
    }

    @Override
    protected BaseRepository getDao() {
        return dao;
    }

    @Override
    protected CmMember model2Entity(Member model) {
        CmMember cmMember = null;
        if (isNotEmpty(model)) {
            cmMember = new CmMember();
            cmMember.setAccount(model.getAccount());
            cmMember.setActive(model.getActive());
            cmMember.setChestline(model.getChestline());
            cmMember.setCode(model.getCode());
            cmMember.setHeadUrl(model.getHeadUrl());
            cmMember.setHight(model.getHight());
            cmMember.setHipline(model.getHipline());
            cmMember.setIntegrate(model.getIntegrate());
            cmMember.setCreateTime(model.getCreateTime());
            cmMember.setInputTime(model.getInputTime());
            cmMember.setLastOrderTime(model.getLastOrderTime());
            cmMember.setLength(model.getLength());
            if (isNotEmpty(model.getMemberGroupId())) {
                CmMemberGroup cmMemberGroup = new CmMemberGroup();
                cmMemberGroup.setId(model.getMemberGroupId());
                cmMember.setMemberGroup(cmMemberGroup);
            }
            if (isNotEmpty(model.getMemberLevelId())) {
                CmMemberLevel cmMemberLevel = new CmMemberLevel();
                cmMemberLevel.setId(model.getMemberLevelId());
                cmMember.setMemberLevel(cmMemberLevel);
            }
            if (isNotEmpty(model.getSurveyorId())) {
                CmSurveyor cmSurveyor = new CmSurveyor();
                cmSurveyor.setId(model.getSurveyorId());
                cmMember.setSurveyor(cmSurveyor);
            }
            cmMember.setNeckline(model.getNeckline());
            cmMember.setOutseam(model.getOutseam());
            cmMember.setPhoneNumber(model.getPhoneNumber());
            cmMember.setRealName(model.getRealName());
            cmMember.setRemark(model.getRemark());
            cmMember.setSex(model.getSex());
            cmMember.setShoulder(model.getShoulder());
            cmMember.setTurnoverMoney(model.getTurnoverMoney());
            cmMember.setTurnoverOrder(model.getTurnoverOrder());
            cmMember.setWaistline(model.getWaistline());
            cmMember.setWeight(model.getWeight());
            cmMember.setPassword(model.getPassword());
            cmMember.setUsername(model.getUsername());
            cmMember.setStatus(model.getStatus());
        }
        return cmMember;
    }

    @Override
    protected Member entity2model(CmMember entity) {
        Member member = null;
        if (isNotEmpty(entity)) {
            member = new Member();
            member.setAccount(entity.getAccount());
            member.setActive(entity.getActive());
            member.setChestline(entity.getChestline());
            member.setCode(entity.getCode());
            member.setHeadUrl(entity.getHeadUrl());
            member.setHight(entity.getHight());
            member.setHipline(entity.getHipline());
            member.setIntegrate(entity.getIntegrate());
            member.setCreateTime(entity.getCreateTime());
            member.setInputTime(entity.getInputTime());
            member.setLastOrderTime(entity.getLastOrderTime());
            member.setLength(entity.getLength());
            member.setMemberGroupId(entity.getMemberGroup().getId());
            member.setMemberLevelId(entity.getMemberLevel().getId());
            if (isNotEmpty(entity.getSurveyor())) {
                member.setSurveyorId(entity.getSurveyor().getId());
                member.setSurveyorName(entity.getSurveyor().getRealName());
            }
            member.setNeckline(entity.getNeckline());
            member.setOutseam(entity.getOutseam());
            member.setPhoneNumber(entity.getPhoneNumber());
            member.setRealName(entity.getRealName());
            member.setRemark(entity.getRemark());
            member.setSex(entity.getSex());
            member.setShoulder(entity.getShoulder());
            member.setTurnoverMoney(entity.getTurnoverMoney());
            member.setTurnoverOrder(entity.getTurnoverOrder());
            member.setWaistline(entity.getWaistline());
            member.setWeight(entity.getWeight());
            member.setPassword(entity.getPassword());
            member.setUsername(entity.getUsername());
            member.setStatus(entity.getStatus());
        }
        return member;
    }

    @Override
    public Member save(Member model) {
        if (model.isNew()) {
            if (isNotEmpty(model.getSurveyorId())) {
                model.setInputTime(new Date());
            }
            model = simpleSave(model);
        } else {
            CmMember cmMember = dao.findOne(model.getId());
            if (isNotEmpty(model.getMemberGroupId())) {
                CmMemberGroup cmMemberGroup = new CmMemberGroup();
                cmMemberGroup.setId(model.getMemberGroupId());
                cmMember.setMemberGroup(cmMemberGroup);
            }
            if (isNotEmpty(model.getActive()))
                cmMember.setActive(model.getActive());
            if (isNotEmpty(model.getChestline()))
                cmMember.setChestline(model.getChestline());
            if (isNotEmpty(model.getHeadUrl()))
                cmMember.setHeadUrl(model.getHeadUrl());
            if (isNotEmpty(model.getHight()))
                cmMember.setHight(model.getHight());
            if (isNotEmpty(model.getHipline()))
                cmMember.setHipline(model.getHipline());
            if (isNotEmpty(model.getLength()))
                cmMember.setLength(model.getLength());
            if (isNotEmpty(model.getNeckline()))
                cmMember.setNeckline(model.getNeckline());
            if (isNotEmpty(model.getOutseam()))
                cmMember.setOutseam(model.getOutseam());
            if (isNotEmpty(model.getPhoneNumber()))
                cmMember.setPhoneNumber(model.getPhoneNumber());
            if (isNotEmpty(model.getRealName()))
                cmMember.setRealName(model.getRealName());
            if (isNotEmpty(model.getRemark()))
                cmMember.setRemark(model.getRemark());
            if (isNotEmpty(model.getSex()))
                cmMember.setSex(model.getSex());
            if (isNotEmpty(model.getShoulder()))
                cmMember.setShoulder(model.getShoulder());
            if (isNotEmpty(model.getWaistline()))
                cmMember.setWaistline(model.getWaistline());
            if (isNotEmpty(model.getWeight()))
                cmMember.setWeight(model.getWeight());

            model = entity2model(dao.save(cmMember));
        }
        return model;
    }
}
